import 'package:flutter/material.dart';
import 'package:myapp/pages/home_page.dart';
import 'package:myapp/pages/my_page.dart';
import 'package:myapp/pages/search_page.dart';
import 'package:myapp/pages/channel_page.dart';
import 'package:myapp/pages/test_page.dart';

class TabNavigator extends StatefulWidget {
  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

final tabList = [
  {
    "text": '首页',
    "icon": Icons.home,
  },
  {
    "text": '搜索',
    "icon": Icons.search,
  },
  {
    "text": '圈子',
    "icon": Icons.camera,
  },
  {
    "text": 'main',
    "icon": Icons.account_circle,
  },
  {
    "text": 'test',
    "icon": Icons.textsms,
  }
];

class _TabNavigatorState extends State<TabNavigator> {
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  int _currentIndex = 0;
  final _controller = PageController(
    initialPage: 0,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: <Widget>[
          HomePage(),
          SearchPage(),
          ChannelPage(),
          MyPage(),
          Test(),
        ],
        onPageChanged: (value) {
          setState(() {
            _currentIndex = value;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
        fixedColor: _activeColor,
        items: tabList
            .asMap()
            .keys
            .map(
              (item) => BottomNavigationBarItem(
                icon: Icon(
                  tabList[item]["icon"],
                  color: _defaultColor,
                ),
                activeIcon: Icon(
                  tabList[item]["icon"],
                  color: _activeColor,
                ),
                label: tabList[item]["text"],
              ),
            )
            .toList(),
      ),
    );
  }
}
